package com.jieshuibao.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.jieshuibao.mapper.MessageExMapper;
import com.jieshuibao.mapper.MessageMapper;
import com.jieshuibao.model.db.Message;
import com.jieshuibao.model.db.MessageExample;
import com.jieshuibao.model.db.MessageExample.Criteria;
import com.jieshuibao.model.domain.PageRequest;
import com.jieshuibao.model.domain.SearchResponse;
import com.jieshuibao.util.IdGen;
import com.jieshuibao.util.StringUtils;
@Service
public class MessageService {
	@Resource
	private MessageMapper messageMapper;
	@Resource
	private MessageExMapper messageExMapper;

	public int insertMessage(Message message){
		if(StringUtils.isBlank(message.getMessageId())){
			message.setMessageId(IdGen.uuid());
		}
		message.setCreateDate(new Date());
		message.setMessageType("1");
		return messageMapper.insertSelective(message);
	}
	public int deleteMessageByMessageId(String id,String userId){
		MessageExample example = new MessageExample();
		Criteria criteria = example.createCriteria();
		criteria.andMessageIdEqualTo(id);
		criteria.andUserIdEqualTo(userId);
		List<Message> list = messageMapper.selectByExample(example);
		if(list != null && list.size() == 1){
			return messageMapper.deleteByExample(example);
		}
		return 0;
	}
	public Message selectMessageById(String id){
		MessageExample example = new MessageExample();
		Criteria criteria = example.createCriteria();
		criteria.andMessageIdEqualTo(id);
		List<Message> messages = messageMapper.selectByExample(example);
		if(messages != null && messages.size() == 1){
			if("0".equals(messages.get(0).getRead())){
				Message m = new Message();
				m.setRead("1");
				messageMapper.updateByExampleSelective(m, example);
				messages.get(0).setRead("1");
			}
			return messages.get(0);
		}
		return null;
	}
	public SearchResponse selectMessagesByMessageType(PageRequest pageRequest){
		SearchResponse searchResponse = new SearchResponse();
		if(StringUtils.isBlank(pageRequest.getMessageType())){
			searchResponse.setCode("3");
			searchResponse.setDes("消息类型不能为空");
			return searchResponse;
		}
		//数据起始索引
		int sinceId = pageRequest.getSinceId();
		//数据结束索引
		int maxId = pageRequest.getMaxId();
		//获取文章作者的手机号（点击个人文章是必填）
		String uuid = pageRequest.getUuid();
		//数据索引-逻辑处理
		if (sinceId <= 0) {
			sinceId = 1;
		}
		if (maxId <= 0 || maxId < sinceId) {
			maxId = sinceId;
		}
		Map<String, Object> paramsMap = new HashMap<>();
		paramsMap.put("pageIndex", sinceId - 1);
		paramsMap.put("pageSize", maxId - sinceId + 1);
		paramsMap.put("messageType", pageRequest.getMessageType());
		paramsMap.put("ifDelete", "0");
		if("1".equals(pageRequest.getMessageType())){//业务消息需要根据用户主键查询
			paramsMap.put("userId", pageRequest.getPhone());
		}
		List<Message> messages = messageExMapper.selectMessagesByMap(paramsMap);
		searchResponse.setDatas(messages);
		searchResponse.setMaxId(maxId);
		searchResponse.setSinceId(sinceId);
		return searchResponse;
	}
	public SearchResponse updateMessagesForRead(String messageId){
		SearchResponse searchResponse = new SearchResponse();
		Message m = new Message();
		m.setMessageId(messageId);
		m.setRead("1");
		MessageExample example = new MessageExample();
		Criteria criteria = example.createCriteria();
		criteria.andMessageIdEqualTo(messageId);
		
		List<Message> list = messageMapper.selectByExample(example);
		if(!"1".equals(list.get(0).getMessageType())){
			searchResponse.setCode("3");
			searchResponse.setDes("请确认操作的是业务推送消息");
			return searchResponse;
		}
		criteria.andMessageTypeEqualTo("1");
		messageMapper.updateByExampleSelective(m,example);
		searchResponse.setCode("1");
		return searchResponse;
	}
	public Map<String, Object> countForRedPoint(Map<String, Object> map){
		return messageExMapper.countForRedPoint(map);
	}
}
